const {
	Controller
} = require('uni-cloud-router')

module.exports = class SubScribeController extends Controller {
	// 结束服务
	async finishWork() {
		let {
			motorcadeId
		} = this.ctx.data
		if (!motorcadeId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.finishWork({
				motorcadeId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 开始服务
	async startWork() {
		let {
			motorcadeId
		} = this.ctx.data
		if (!motorcadeId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.startWork({
				motorcadeId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 确认接单
	async confirmOrder() {
		let {
			motorcadeId
		} = this.ctx.data
		if (!motorcadeId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.confirmOrder({
				motorcadeId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 拒绝接单
	async confirmOrder() {
		let {
			motorcadeId
		} = this.ctx.data
		if (!motorcadeId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.confirmOrder({
				motorcadeId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 拒绝接单
	async refuseOrder() {
		let {
			motorcadeId,
			reason
		} = this.ctx.data
		if (!motorcadeId || !reason || !reason.trim()) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.refuseOrder({
				motorcadeId,
				reason
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 取消车队
	async cancelMotorcade() {
		let {
			motorcadeId,
			reason
		} = this.ctx.data
		if (!motorcadeId || !reason || !reason.trim()) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.cancelMotorcade({
				motorcadeId,
				reason
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
	
	// 分配主持人
	async assignCompere() {
		let {
			motorcadeId,
			compereId
		} = this.ctx.data
		if (!motorcadeId || !compereId) {
			return {
				code: 500,
				msg: "参数错误"
			}
		}
		try {
			await this.service.subscribe.assignCompere({
				motorcadeId,
				compereId
			})
			return {
				code: 200
			}
		} catch (e) {
			return {
				code: 500,
				msg: e.message
			}
		}
	}
  
  // 合作门店老板接车队单
  async cooperativeStoreReceiveOrder() {
  	let { motorcadeId } = this.ctx.data
  	if (!motorcadeId) {
  		return {
  			code: 500,
  			msg: "参数错误"
  		}
  	}
  	try {
  		await this.service.subscribe.cooperativeStoreReceiveOrder({ motorcadeId })
  		return {
  			code: 200
  		}
  	} catch (e) {
  		return {
  			code: 500,
  			msg: e
  		}
  	}
  }
  
  // 合作门店老板结束车队单
  async endService() {
    let { motorcadeId } = this.ctx.data
    
    if (!motorcadeId) return { code: 500, msg: "参数错误" }
    
    return await this.service.subscribe.endService({ motorcadeId })
  }
  
  // 共享超管修改合作门店车队订单
  async modifyOrder() {
    let { motorcadeId, storeId, roomId } = this.ctx.data
    
    if (!motorcadeId && !storeId) return { code: 500, msg: "参数错误" }
    
    return await this.service.subscribe.modifyOrder({ motorcadeId, storeId, roomId })
  }
  
  // 确认接单（商品车队）
  async confirmGoodsOrder() {
  	let {
  		motorcadeId
  	} = this.ctx.data
  	if (!motorcadeId) {
  		return {
  			code: 500,
  			msg: "参数错误"
  		}
  	}
  	try {
  		await this.service.subscribe.confirmGoodsOrder({
  			motorcadeId
  		})
  		return {
  			code: 200
  		}
  	} catch (e) {
  		return {
  			code: 500,
  			msg: e.message
  		}
  	}
  }
  
  // 拒绝接单(商品车队)
  async refuseGoodsOrder() {
  	let {
  		motorcadeId,
  		reason
  	} = this.ctx.data
  	if (!motorcadeId || !reason || !reason.trim()) {
  		return {
  			code: 500,
  			msg: "参数错误"
  		}
  	}
  	try {
  		await this.service.subscribe.refuseGoodsOrder({
  			motorcadeId,
  			reason
  		})
  		return {
  			code: 200
  		}
  	} catch (e) {
  		return {
  			code: 500,
  			msg: e.message
  		}
  	}
  }
  
}

